﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Data;
using mesoBoard.Data.Repositories;
using System.Web.Caching;
using System.Web.Mvc.Html;
using Microsoft.Web.Mvc;
using System.Data;

namespace mesoBoard.Common.HtmlHelpers
{
    public static partial class mesoBoardHtmlHelpers
    {
        public static int GetNewMessagesCount(this HtmlHelper html)
        {
            return (int)html.ViewData["mbNewMessagesCount"];
        }

        public static string GetInboxLinkText(this HtmlHelper html)
        {
            int newCount = html.GetNewMessagesCount();
            if (newCount > 0)
                return string.Format("Inbox <strong>[{0}]</strong>", newCount.ToString());
            else
                return "Inbox";
        }

        public static string GetLogoutLinkText(this HtmlHelper html)
        {
            return string.Format("Logout <strong>[{0}]</strong>", html.GetCurrentUser().Username);
        }

        public static User GetCurrentUser(this HtmlHelper html)
        {
            return (User)html.ViewData["mbCurrentUser"];
        }

        public static string UsernameLink(this HtmlHelper html, User TheUser, bool IncludeRankRolor=true)
        {
            if (IncludeRankRolor)
            {
                string colorHex = html.UsernameColor(TheUser);
                object styleObject = colorHex != string.Empty ? new { style = colorHex } : null;
                return html.ActionLink(TheUser.Username, "UserProfile", "Members", new { UserNameOrID = TheUser.UserID }, styleObject).ToString();
            }
            else
                return html.ActionLink(TheUser.Username, "UserProfile", "Members", new { UserNameOrID = TheUser.UserID }, null).ToString();
        }

        public static string UsernameColor(this HtmlHelper html, User TheUser)
        {
            if (TheUser == null || TheUser.UserID == 0)
                return string.Empty;

            RoleRepository RoleRep = new RoleRepository(new mbEntities());

            if (TheUser.UserProfile.DefaultRole.HasValue)
            {
                Role TheRole = RoleRep.GetRole(TheUser.UserProfile.DefaultRole.Value);
                return TheRole.Rank.Color;
            }
            else
                return string.Empty;
        }

        public static string UserRank(this HtmlHelper html, User TheUser)
        {
            string RankTitle = "<div class='title' style='color:{0}'>{1}</div>";
            string RankImage = "<div class='image'>{0}</div>";

            Rank TheRank;

            RankRepository RankRep = new RankRepository(new mbEntities());
            RoleRepository RoleRep = new RoleRepository(new mbEntities());

            if (TheUser.UserProfile.DefaultRole.HasValue)
            {
                Role TheRole = RoleRep.GetRole(TheUser.UserProfile.DefaultRole.Value);
                TheRank = TheRole.Rank;
            }
            else
                TheRank = RankRep.GetRank(PostCount: TheUser.TotalPosts);

            if (TheRank == null)
                return "";

            string RankTitleParsed = string.Format(RankTitle, TheRank.Color, TheRank.Title);

            if (TheRank == null)
                return "";
            else if (string.IsNullOrWhiteSpace(TheRank.Image))
                return RankTitleParsed;

            string img = "";
            for (int i = 1; i <= TheRank.Repeat; i++)
            {
                img += html.Image("~/Images/Ranks/" + TheRank.Image).ToString() + "&nbsp;";
            }

            return RankTitleParsed + string.Format(RankImage, img);
        }
    }
}
